<template>
  <div class="search">
    <div class="top">
      <span @click='backPath'> <i class="iconfont icon-fanhui" style='font-weight:bold;font-size:20px'></i> </span>
      <div class="top-center">
        <i class="iconfont icon-sousuo"></i>
        <input type="text" ref='input' :placeholder="msg" @input='getUserC' v-model='searchData'/>
        <i class="iconfont icon-guanbi" v-if='searchData==""?"":"showShop"' @click='clear'></i>
      </div>
      <button>搜索</button>
    </div>
    <div class="showList" v-if='searchData==""?"":"showShop"'>
        <div class="shopDiv" v-if='showShop'>
          <i class="iconfont icon-jinrudianpu"></i> "{{searchList[0].name}}"店铺
        </div>
        <div class="itemSearch" v-for='(item,index) in searchList' :key='index'>
          <p class="left">{{item.name}}</p>
          <div class="rightClass">
            <span v-for='(items,indexs) in item.list' :key='indexs'>{{items.name}}</span>
          </div>
        </div>
    </div>
  </div>
</template>

<script>
import {searchS} from '@/http/api/search'
export default {
   data(){
    return{
        msg:'',
        showClear:false,//显示清除按钮
        showShop:true,//显示店铺
        searchData:'',//用户输入的实时数据
        searchList:[],//接受请求返回的数据
    }
    },
    methods:{
        backPath(){
            this.$router.go(-1)
        },
        getUserC(){ //input的input事件
          // console.log(this.searchData);
          if(this.searchData){
            this.showClear=true;
            this.getSearch();
          }
        },
        getSearch(){  //根据用户输入的内容发送请求进行模糊查找
          searchS('/search',{msg:this.searchData}).then(res=>{
            // console.log(res);
            this.searchList = res.searchList; //
            this.shopShow = res.searchList[0]
            console.log(this.searchList);
          })
        },
        clear(){  //清除按钮
            this.searchData='',
            this.showClear=false
        }
    },
    mounted() {
    // dom结构在浏览器上生成,自动获取焦点
    this.$refs.input.focus();
    // console.log( this.$refs);
    this.msg=this.$route.query.msg
  },
};
</script>

<style scoped lang ='scss'>
.search {
  height: 100%;
  .top {
    display: flex;
    justify-content: space-around;
    align-items: center;
    margin-top: 20px;
    border-bottom: 3px solid #ccc;
    height: 200px;
    span {
      font-size: 120px;
    }
    .top-center{
        display: flex;
        align-items: center;
        width: 60%;
        height: 85%;
        border-radius: 50px;
        background: #F7F7F7;
        i{
            margin-left: 50px;
        }
        input{
            border: none;
            height: 100%;
            background: #F7F7F7;
             border-radius: 50px;
        }
        .icon-guanbi{
          font-size: 100px!important;
          margin-right: 30px;
        }
    }
    button {
      background: red;
      color: white;
      border-radius: 30px;
      border: none;
    }
    }
    .itemSearch{
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 100px;
    }
    .showList{
      .shopDiv{
        margin-left: 50px;
        margin-top: 50px;
      }
      .icon-jinrudianpu{
        text-align: center;
        font-size: 100px!important;
      }
      .rightClass{
        background: #F0F2F5;
        padding: 30px;
        border-radius: 30px;
      }
    }
  }

</style>